<?php

session_start();
if(!array_key_exists("user", $_SESSION) || $_SESSION['account'] != 1){
	header("Location: ../common/redirect.php");
	exit;
}

if(!array_key_exists("exam_id", $_POST) && !array_key_exists("current_exam_id", $_POST)){
	header("Location: test.php");
}

include_once("../class/db.php");
$db = new db();
if(!array_key_exists("exam", $_SESSION)){
	$sql = "SELECT uid, owner, title, contenttype, question_type, question_quantity, question_source, automatic_score, question_timer, question_display, calculator, start_date, end_date FROM course_content WHERE id = {$_POST['exam_id']} AND contenttype = 'exam'";
	$exam_data = $db->execute($sql, "onerow");
	
	if($exam_data == false){
		//header("Location: test.php");
		echo "No hay resultados :(";
	}
	
	//Geeting the questions
	$question_type = explode(",", $exam_data['question_type']);
	$question_quantity = explode(",", $exam_data['question_quantity']);
	$questions_holder = array();
	if($exam_data['question_source'] == "public"){
		if(count($question_type) > 1){
			foreach($question_type as $key => $value){
				$sql = "SELECT * FROM questions WHERE uid = {$exam_data['uid']} AND question_type = '$value' ORDER BY RAND() LIMIT {$question_quantity[$key]}";
				$questions_temp_result = $db->execute($sql, "all");
				if($questions_temp_result == false){
					//header("Location: test.php?questions=Not enough questions to take this test. Please contact your teacher");
					echo "No hay resultados en preguntas publicas :(";
					exit;
				} else {
					foreach($questions_temp_result as $temp_result){
						$questions_holder[$value][] = $temp_result; 
						$_SESSION['exam']['questions_quantity_answered'][$key] = 0;
					}
				}
			}
		}
	} else {
		if(count($question_type) > 1){
			foreach($question_type as $key => $value){
				$sql = "SELECT * FROM questions WHERE uid = {$exam_data['uid']} AND owner = {$exam_data['owner']} AND question_type = '$value' ORDER BY RAND() LIMIT {$question_quantity[$key]}";
				$questions_temp_result = $db->execute($sql, "all");
				if($questions_temp_result == false){
					//header("Location: test.php?questions=Not enough questions to take this test. Please contact your teacher");
					echo "No hay resultados en preguntas propietarias :(";
					exit;
				} else {
					foreach($questions_temp_result as $temp_result){
						$questions_holder[$value][] = $temp_result; 
						$_SESSION['exam']['questions_quantity_answered'][$key] = 0;
					}
				}
			}
		}
	}
	
	//Validate the current questions on the database are anough to apply the test
	foreach($question_quantity as $key => $value){
		if($value <= $exam_data['question_display']){
			//header("Location: test.php?questions=Not enough questions to take this test. Please contact your teacher");
			//exit;
		}
	}
	
	//Setting up the exam
	$_SESSION['exam']['questions_type'] = $question_type;
	$_SESSION['exam']['questions_quantity'] = $question_quantity;
	$_SESSION['exam']['question_display'] = $exam_data['question_display'];
	$_SESSION['exam']['loop'] = 0;
	$_SESSION['exam']['correct_asnwers'] = 0;
	$_SESSION['exam']['total_questions'] = 0;
	$_SESSION['exam']['answered_questions'] = array();
	$_SESSION['exam']['uid'] = $exam_data['uid'];
	$_SESSION['exam']['title'] = $exam_data['title'];
	$_SESSION['exam']['automatic_score'] = $exam_data['automatic_score'];
	$_SESSION['exam']['question_timer'] = $exam_data['question_timer'];
	$_SESSION['exam']['calculator'] = $exam_data['calculator'];
	$_SESSION['exam']['questions'] = $questions_holder;
}

if(array_key_exists("current_exam_id", $_POST)){
	switch($_POST['form_question_type']){
		case "optional":
			foreach($_POST as $question_key => $question_value){
				if($question_key == "button" || $question_key == "current_exam_id" || $question_key == "form_question_type"){
					continue;
				}
				$data_holder = explode("_", $question_key);
				foreach($_SESSION['exam']['answered_questions'] as $answered_questions_value){
					if($answered_questions_value['id'] == $data_holder[1]){
						$current_question = $answered_questions_value['question'];
						$current_user_answer = $question_value;
						$current_system_asnwer = $answered_questions_value['answer'];
						if($answered_questions_value['answer'] == $question_value){
							$_SESSION['exam']['correct_asnwers']++;
						}
					}
				}
				$sql = "INSERT INTO exam_questions (exam_id, username, question, question_id, user_answer, right_answer) VALUES ({$_POST['current_exam_id']}, {$_SESSION['user']}, '$current_question', {$data_holder[1]}, '$current_user_answer', '$current_system_asnwer')";
				$_SESSION['exam']['total_questions']++;
				$db->execute($sql, "none");
			}
			break;
		case "open":
			foreach($_POST as $question_key => $question_value){
				if($question_key == "button" || $question_key == "current_exam_id" || $question_key == "form_question_type"){
					continue;
				}
				foreach($_SESSION['exam']['answered_questions'] as $answered_questions_value){
					$current_question = $answered_questions_value['question'];
					$_SESSION['exam']['correct_asnwers']++;
				}
				$data_holder = explode("_", $question_key);
				$sql = "INSERT INTO exam_questions (exam_id, username, question, question_id, user_answer) VALUES ({$_POST['current_exam_id']}, {$_SESSION['user']}, '$current_question', {$data_holder[1]}, '$question_value')";
				$_SESSION['exam']['total_questions']++;
				$db->execute($sql, "none");
			}
			break;
		case "multiple":
			foreach($_SESSION['exam']['answered_questions'] as $session_questions){
				$current_answer_id = "answer_".$session_questions['id'];
				if(array_key_exists($current_answer_id, $_POST)){
					$current_question = $session_questions['question'];
					$current_user_answer = implode(",", $_POST[$current_answer_id]);
					$current_system_asnwer = $session_questions['answer'];
					$current_question_correct_counter = 0;
					$current_questions_in_the_system = 1;
					$total_score = 0;
					if(in_array($session_questions['answer'], $_POST[$current_answer_id])){
						$current_question_correct_counter++;
					}
					if($session_questions['answera'] != ""){
						$current_questions_in_the_system++;
						$current_system_asnwer = $current_system_asnwer.", ".$session_questions['answera'];
						if(in_array($session_questions['answera'], $_POST[$current_answer_id])){
							$current_question_correct_counter++;
						}
					}
					if($session_questions['answerb'] != ""){
						$current_questions_in_the_system++;
						$current_system_asnwer = $current_system_asnwer.", ".$session_questions['answerb'];
						if(in_array($session_questions['answerb'], $_POST[$current_answer_id])){
							$current_question_correct_counter++;
						}
					}
					if($session_questions['answerc'] != ""){
						$current_questions_in_the_system++;
						$current_system_asnwer = $current_system_asnwer.", ".$session_questions['answerc'];
						if(in_array($session_questions['answerc'], $_POST[$current_answer_id])){
							$current_question_correct_counter++;
						}
					}
					if(in_array($session_questions['falsea'], $_POST[$current_answer_id])){
						$current_question_correct_counter--;
					}
					if($session_questions['falseb'] != "" && in_array($session_questions['falseb'], $_POST[$current_answer_id])){
						$current_question_correct_counter--;
					}
					if($session_questions['falsec'] != "" && in_array($session_questions['falsec'], $_POST[$current_answer_id])){
						$current_question_correct_counter--;
					}
					if($session_questions['falsed'] != "" && in_array($session_questions['falsed'], $_POST[$current_answer_id])){
						$current_question_correct_counter--;
					}
					if($current_question_correct_counter <= 0){
						$total_score += 0;
					} else {
						$total_score += $current_question_correct_counter / $current_questions_in_the_system;
					}
					$_SESSION['exam']['correct_asnwers'] += $total_score;
				} else {
					$current_user_answer = "Not Answered";
					$current_system_asnwer = $session_questions['answer'];
					if($session_questions['answera'] != ""){
						$current_system_asnwer = $current_system_asnwer.", ".$session_questions['answera'];
					}
					if($session_questions['answerb'] != ""){
						$current_system_asnwer = $current_system_asnwer.", ".$session_questions['answerb'];
					}
					if($session_questions['answerc'] != ""){
						$current_system_asnwer = $current_system_asnwer.", ".$session_questions['answerc'];
					}
				}
				$sql = "INSERT INTO exam_questions (exam_id, username, question, question_id, user_answer, right_answer) VALUES ({$_POST['current_exam_id']}, {$_SESSION['user']}, '$current_question', {$session_questions['id']}, '$current_user_answer', '$current_system_asnwer')";
				$_SESSION['exam']['total_questions']++;
				$db->execute($sql, "none");
			}
			break;
		case "essays":
			foreach($_POST as $question_key => $question_value){
				if($question_key == "button" || $question_key == "current_exam_id" || $question_key == "form_question_type"){
					continue;
				}
				foreach($_SESSION['exam']['answered_questions'] as $answered_questions_value){
					$current_question = $answered_questions_value['question'];
					$_SESSION['exam']['correct_asnwers']++;
				}
				$data_holder = explode("_", $question_key);
				$sql = "INSERT INTO exam_questions (exam_id, username, question, question_id, user_answer) VALUES ({$_POST['current_exam_id']}, {$_SESSION['user']}, '$current_question', {$data_holder[1]}, '$question_value')";
				$_SESSION['exam']['total_questions']++;
				$db->execute($sql, "none");
			}
			break;
		case "problems":
			foreach($_POST as $question_key => $question_value){
				if($question_key == "button" || $question_key == "current_exam_id" || $question_key == "form_question_type"){
					continue;
				}
				foreach($_SESSION['exam']['answered_questions'] as $answered_questions_value){
					$current_question = $answered_questions_value['question'];
					$_SESSION['exam']['correct_asnwers']++;
				}
				$data_holder = explode("_", $question_key);
				$sql = "INSERT INTO exam_questions (exam_id, username, question, question_id, user_answer) VALUES ({$_POST['current_exam_id']}, {$_SESSION['user']}, '$current_question', {$data_holder[1]}, '$question_value')";
				$_SESSION['exam']['total_questions']++;
				$db->execute($sql, "none");
			}
			break;
		case "truefalse":
			foreach($_POST as $question_key => $question_value){
				if($question_key == "button" || $question_key == "current_exam_id" || $question_key == "form_question_type"){
					continue;
				}
				$data_holder = explode("_", $question_key);
				foreach($_SESSION['exam']['answered_questions'] as $answered_questions_value){
					if($answered_questions_value['id'] == $data_holder[1]){
						$current_question = $answered_questions_value['question'];
						$current_user_answer = $question_value;
						$current_system_asnwer = $answered_questions_value['answer'];
						if($answered_questions_value['answer'] == $question_value){
							$_SESSION['exam']['correct_asnwers']++;
						}
					}
				}
				$sql = "INSERT INTO exam_questions (exam_id, username, question, question_id, user_answer, right_answer) VALUES ({$_POST['current_exam_id']}, {$_SESSION['user']}, '$current_question', {$data_holder[1]}, '$current_user_answer', '$current_system_asnwer')";
				$_SESSION['exam']['total_questions']++;
				$db->execute($sql, "none");
			}
			break;
		case "match":
			foreach($_POST as $question_key => $question_value){
				if($question_key == "button" || $question_key == "current_exam_id" || $question_key == "form_question_type"){
					continue;
				}
				$data_holder = explode("_", $question_key);
				foreach($_SESSION['exam']['answered_questions'] as $answered_questions_value){
					if($answered_questions_value['id'] == $data_holder[1]){
						$current_question = $answered_questions_value['question'];
						$current_user_answer = $question_value;
						$current_system_asnwer = $answered_questions_value['answer'];
						if($answered_questions_value['answer'] == $question_value){
							$_SESSION['exam']['correct_asnwers']++;
						}
					}
				}
				$sql = "INSERT INTO exam_questions (exam_id, username, question, question_id, user_answer, right_answer) VALUES ({$_POST['current_exam_id']}, {$_SESSION['user']}, '$current_question', {$data_holder[1]}, '$current_user_answer', '$current_system_asnwer')";
				$_SESSION['exam']['total_questions']++;
				$db->execute($sql, "none");
			}
			break;
	}
	$_SESSION['exam']['questions_quantity_answered'][$_SESSION['exam']['loop']] += count($_SESSION['exam']['answered_questions']);
}
if($_SESSION['exam']['questions_quantity_answered'][$_SESSION['exam']['loop']] >= $_SESSION['exam']['questions_quantity'][$_SESSION['exam']['loop']]){
	$_SESSION['exam']['loop']++;
	if($_SESSION['exam']['loop'] >= count($_SESSION['exam']['questions_type'])){
		header("Location: test_done.php");
		exit;
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PIE EDUCATION | <?php echo $_SESSION['exam']['title']; ?></title>
<link rel="stylesheet" type="text/css" href="/css/style-student.css" />
<link href="/css/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="/js/index.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<?php

if($_SESSION['exam']['question_timer'] != 0){
$miliseconds_per_question = $_SESSION['exam']['question_timer'] * 1000;
if(($_SESSION['exam']['questions_quantity'][$_SESSION['exam']['loop']] - $_SESSION['exam']['questions_quantity_answered'][$_SESSION['exam']['loop']]) > $_SESSION['exam']['question_display']){
	$current_questions_to_display = $_SESSION['exam']['question_display'];
	$timer = ($current_questions_to_display * $_SESSION['exam']['question_timer']) * 1000;
} else {
	$current_questions_to_display = $_SESSION['exam']['questions_quantity'][$_SESSION['exam']['loop']] - $_SESSION['exam']['questions_quantity_answered'][$_SESSION['exam']['loop']];
	$timer = ($current_questions_to_display * $_SESSION['exam']['question_timer']) * 1000;
}

echo '
<script language="Javascript1.2">
	<!--
	setTimeout(\'document.'.$_SESSION['exam']['questions_type'][$_SESSION['exam']['loop']].'_questions.submit();\','.$timer.');
	// -->
</script>
';
}
?>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-35230372-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<script>
<?php
if($_SESSION['exam']['questions_type'][$_SESSION['exam']['loop']] == "match"){
	$questions = array_splice($_SESSION['exam']['questions'][$_SESSION['exam']['questions_type'][$_SESSION['exam']['loop']]], 0, $current_questions_to_display);
	$_SESSION['exam']['answered_questions'] = $questions;
echo "
$(document).ready(function() {";
$counter = 0;
foreach($questions as $drag_content){
echo "
	$('#draggable_$counter').draggable();
	$('#droppable_{$drag_content['id']}').droppable({
		drop: function(event, ui) { 
			var id = $(ui.draggable).text();
			var temp_self_id = $(this).attr('id');
			var self_id = temp_self_id.split('_');
			var target_input = document.getElementById('answer_' + self_id[1]);
			target_input.value = id;
			$(this).css('background-color','red');
		}
	});
	$('#droppable_{$drag_content['id']}').droppable({
		out: function(event, ui) { 
			$(this).css('background-color','green');
			var temp_self_id = $(this).attr('id');
			var self_id = temp_self_id.split('_');
			var target_input = document.getElementById('answer_' + self_id[1]);
			target_input.value = 0;
		}
	});";
	$counter++;
}
echo "
});
";
}
?>
</script>
</head>

<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td bgcolor="#000000"><table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td width="350" height="30">&nbsp;</td>
        <td align="right" valign="bottom"><a href="class.php" class="top-menu" style="color:#999">MY COURSES</a> <a href="schedule.php" class="top-menu" style="color:#999">CALENDAR</a> <a href="homework.php" class="top-menu" style="color:#999">HOMEWORK</a> <a href="test.php" class="top-menu" style="color:#999">EXAMS</a>  <a href="history.php" class="top-menu" style="color:#999">RECORDS</a> <a href="tools.php" class="top-menu" style="color:#999">TOOLS</a> <a href="/logout.php" class="top-menu" style="color:#999">SIGN OUT</a></td>
      </tr>
	  <tr>
        <td>&nbsp;</td>
        <td align="right" valign="bottom"><a href="/profile/" class="login-menu" style="color:#FFF"><?php echo $_SESSION['name'].' '.$_SESSION['lname']; ?></a> <a href="/common/settings.php" class="login-menu" style="color:#FFF">Change Settings</a> <a href="/common/redirect.php" class="login-menu" style="color:#FFF">My Documents</a> <a href="/network/" class="login-menu" style="color:#FFF">My Network</a></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="30" style="background-image:url(/images/top-bg-student.png); background-repeat:repeat-x">&nbsp;</td>
  </tr>
  <tr>
    <td height="700" valign="top"><table width="980" border="0" align="center" cellpadding="0" cellspacing="0" id="main_container">
      <tr>
        <td>
        <?php if($_SESSION['exam']['questions_type'][$_SESSION['exam']['loop']] == "optional"){ ?>
        <form id="optional_questions" name="optional_questions" method="post" action="take_test.php">
          <table width="100%" border="0" cellpadding="8" cellspacing="2" id="optional_questions">
          <tr>
            <td colspan="3" align="center" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:18px; color:#999"><strong>Optional Choice Questions</strong></td>
            </tr>
          <?php
		  $questions = array_splice($_SESSION['exam']['questions'][$_SESSION['exam']['questions_type'][$_SESSION['exam']['loop']]], 0, $current_questions_to_display);
		  $_SESSION['exam']['answered_questions'] = $questions;
		  foreach($questions as $question){
			  echo '<tr>
				<td width="150" align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#999"><strong>Question</strong></td>
				<td width="40" align="center" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">&nbsp;</td>
				<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#00F">'.$question['question'].'</td>
				</tr>';
				$rand = rand(1, 4);
				switch($rand){
			  case 1:
				  echo '<tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
					<td align="center" bgcolor="#EEEEEE"><input type="radio" name="answer_'.$question['id'].'" id="answer_'.$question['id'].'" value="'.$question['answer'].'" />
					  <label for="answer_"></label></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['answer'].'</td>
					</tr>
				  <tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
					<td align="center" bgcolor="#EEEEEE"><input type="radio" name="answer_'.$question['id'].'" id="answer_'.$question['id'].'" value="'.$question['falsea'].'" /></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['falsea'].'</td>
					</tr>
				  <tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
					<td align="center" bgcolor="#EEEEEE"><input type="radio" name="answer_'.$question['id'].'" id="answer_'.$question['id'].'" value="'.$question['falseb'].'" /></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['falseb'].'</td>
					</tr>
				  <tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
					<td align="center" bgcolor="#EEEEEE"><input type="radio" name="answer_'.$question['id'].'" id="answer_'.$question['id'].'" value="'.$question['falsec'].'" /></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['falsec'].'</td>
				  </tr>';
				  break;
			  case 2:
				  echo '<tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
					<td align="center" bgcolor="#EEEEEE"><input type="radio" name="answer_'.$question['id'].'" id="answer_'.$question['id'].'" value="'.$question['falsea'].'" /></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['falsea'].'</td>
					</tr>
				  <tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
					<td align="center" bgcolor="#EEEEEE"><input type="radio" name="answer_'.$question['id'].'" id="answer_'.$question['id'].'" value="'.$question['answer'].'" /></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['answer'].'</td>
					</tr>
				  <tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
					<td align="center" bgcolor="#EEEEEE"><input type="radio" name="answer_'.$question['id'].'" id="answer_'.$question['id'].'" value="'.$question['falseb'].'" /></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['falseb'].'</td>
					</tr>
				  <tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
					<td align="center" bgcolor="#EEEEEE"><input type="radio" name="answer_'.$question['id'].'" id="answer_'.$question['id'].'" value="'.$question['falsec'].'" /></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['falsec'].'</td>
				  </tr>';
				  break;
			  case 3:
				  echo '<tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
					<td align="center" bgcolor="#EEEEEE"><input type="radio" name="answer_'.$question['id'].'" id="answer_'.$question['id'].'" value="'.$question['falsea'].'" />
					  <label for="answer_"></label></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['falsea'].'</td>
					</tr>
				  <tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
					<td align="center" bgcolor="#EEEEEE"><input type="radio" name="answer_'.$question['id'].'" id="answer_'.$question['id'].'" value="'.$question['falseb'].'" /></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['falseb'].'</td>
					</tr>
				  <tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
					<td align="center" bgcolor="#EEEEEE"><input type="radio" name="answer_'.$question['id'].'" id="answer_'.$question['id'].'" value="'.$question['answer'].'" /></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['answer'].'</td>
					</tr>
				  <tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
					<td align="center" bgcolor="#EEEEEE"><input type="radio" name="answer_'.$question['id'].'" id="answer_'.$question['id'].'" value="'.$question['falsec'].'" /></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['falsec'].'</td>
				  </tr>';
				  break;
			  case 4:
				  echo '<tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
					<td align="center" bgcolor="#EEEEEE"><input type="radio" name="answer_'.$question['id'].'" id="answer_'.$question['id'].'" value="'.$question['falsea'].'" />
					  <label for="answer_"></label></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['falsea'].'</td>
					</tr>
				  <tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
					<td align="center" bgcolor="#EEEEEE"><input type="radio" name="answer_'.$question['id'].'" id="answer_'.$question['id'].'" value="'.$question['falseb'].'" /></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['falseb'].'</td>
					</tr>
				  <tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
					<td align="center" bgcolor="#EEEEEE"><input type="radio" name="answer_'.$question['id'].'" id="answer_'.$question['id'].'" value="'.$question['falsec'].'" /></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['falsec'].'</td>
					</tr>
				  <tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
					<td align="center" bgcolor="#EEEEEE"><input type="radio" name="answer_'.$question['id'].'" id="answer_'.$question['id'].'" value="'.$question['answer'].'" /></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['answer'].'</td>
				  </tr>';
				  break;
				}
			  
			  echo '<tr>
				<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
				<td align="center" bgcolor="#EEEEEE"><input name="answer_'.$question['id'].'" type="radio" id="answer_'.$question['id'].'" value="null" checked="checked" /></td>
				<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">No Answer</td>
				</tr>
				<tr>
				<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px">&nbsp;</td>
				<td align="center" bgcolor="#EEEEEE">&nbsp;</td>
				<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">&nbsp;</td>
				</tr>';
		  }
            ?>
          <tr>
            <td colspan="3" align="center" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#F00">Do not press the button below until  you finish to answer all the questions.</td>
            </tr>
          <tr>
            <td colspan="3" align="center" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px"><input name="button" type="submit" class="submit-center" id="button" value="Save Answers" onClick="return confirm('Are you sure you want to proceed. Click cancel to review your answers');" /><input name="current_exam_id" type="hidden" id="current_exam_id" value="<?php echo $_SESSION['exam']['uid']; ?>" />
              <input name="form_question_type" type="hidden" id="form_question_type" value="optional" /></td>
            </tr>
          </table>
        </form>
		<?php 
		}
		if($_SESSION['exam']['questions_type'][$_SESSION['exam']['loop']] == "open"){ 
		?>
          <form id="open_questions" name="open_questions" method="post" action="take_test.php">
            <table width="100%" border="0" cellpadding="8" cellspacing="2" id="open_questions">
              <tr>
                <td colspan="2" align="center" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:18px; color:#999"><strong>Open Answer Questions</strong></td>
                </tr>
              <?php
				$questions = array_splice($_SESSION['exam']['questions'][$_SESSION['exam']['questions_type'][$_SESSION['exam']['loop']]], 0, $current_questions_to_display);
				$_SESSION['exam']['answered_questions'] = $questions;
				foreach($questions as $question){
				  echo '<tr>
					<td width="150" align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#999"><strong>Pregunta</strong></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#00F">'.$question['question'].'</td>
				  </tr>
				  <tr>
					<td align="right" valign="top" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#999"><strong>Answer</strong></td>
					<td bgcolor="#EEEEEE"><textarea name="answer_'.$question['id'].'" cols="45" rows="10" class="form-exam" id="answer_'.$question['id'].'"></textarea></td>
				  </tr>';
				  }
              ?>
              <tr>
                <td colspan="2" bgcolor="#EEEEEE" align="center" style="font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#F00">Do not press the button below until  you finish to answer all the questions.</td>
                </tr>
              <tr>
                <td colspan="2" bgcolor="#EEEEEE" align="center"><input name="button" type="submit" class="submit-center" id="button" value="Save Answers" onClick="return confirm('Are you sure you want to proceed. Click cancel to review your answers');" /><input name="current_exam_id" type="hidden" id="current_exam_id" value="<?php echo $_SESSION['exam']['uid']; ?>" /><input name="form_question_type" type="hidden" id="form_question_type" value="open" /></td>
                </tr>
              </table>
          </form>
		<?php 
		}
		if($_SESSION['exam']['questions_type'][$_SESSION['exam']['loop']] == "multiple"){ 
		?>
          <form id="multiple_questions" name="multiple_questions" method="post" action="take_test.php">
            <table width="100%" border="0" cellspacing="2" cellpadding="8">
              <tr>
                <td colspan="3" align="center" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:18px; color:#999"><strong>Multiple Choice Questions</strong></td>
                </tr>
              <?php
				//pendiente
				$questions = array_splice($_SESSION['exam']['questions'][$_SESSION['exam']['questions_type'][$_SESSION['exam']['loop']]], 0, $current_questions_to_display);
				$_SESSION['exam']['answered_questions'] = $questions;
				foreach($questions as $question){
					$display_options_html = array();
					$display_options_html[] = '<tr>
					<td bgcolor="#EEEEEE">&nbsp;</td>
					<td bgcolor="#EEEEEE" align="center"><input name="answer_'.$question['id'].'[]" type="checkbox" id="answer_'.$question['id'].'[]" value="'.$question['answer'].'" />
					  <label for="answer_5"></label></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['answer'].'</td>
				  </tr>';
				  
					$display_options_html[] = '<tr>
					<td bgcolor="#EEEEEE">&nbsp;</td>
					<td bgcolor="#EEEEEE" align="center"><input name="answer_'.$question['id'].'[]" type="checkbox" id="answer_'.$question['id'].'[]" value="'.$question['answer'].'" />
					  <label for="answer_5"></label></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['answer'].'</td>
				  </tr>';
					$display_options_html[] = '<tr>
					<td bgcolor="#EEEEEE">&nbsp;</td>
					<td bgcolor="#EEEEEE" align="center"><input name="answer_'.$question['id'].'[]" type="checkbox" id="answer_'.$question['id'].'[]" value="'.$question['falsea'].'" />
					  <label for="answer_5"></label></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['falsea'].'</td>
				  </tr>';
				  if($question['answera'] != ""){
					$display_options_html[] = '<tr>
					<td bgcolor="#EEEEEE">&nbsp;</td>
					<td bgcolor="#EEEEEE" align="center"><input name="answer_'.$question['id'].'[]" type="checkbox" id="answer_'.$question['id'].'[]" value="'.$question['answera'].'" />
					  <label for="answer_5"></label></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['answera'].'</td>
				  </tr>';
				  }
				  if($question['answerb'] != ""){
					$display_options_html[] = '<tr>
					<td bgcolor="#EEEEEE">&nbsp;</td>
					<td bgcolor="#EEEEEE" align="center"><input name="answer_'.$question['id'].'[]" type="checkbox" id="answer_'.$question['id'].'[]" value="'.$question['answerb'].'" />
					  <label for="answer_5"></label></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['answerb'].'</td>
				  </tr>';
				  }
				  if($question['answerc'] != ""){
					$display_options_html[] = '<tr>
					<td bgcolor="#EEEEEE">&nbsp;</td>
					<td bgcolor="#EEEEEE" align="center"><input name="answer_'.$question['id'].'[]" type="checkbox" id="answer_'.$question['id'].'[]" value="'.$question['answerc'].'" />
					  <label for="answer_5"></label></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['answerc'].'</td>
				  </tr>';
				  }
				  if($question['falseb'] != ""){
					$display_options_html[] = '<tr>
					<td bgcolor="#EEEEEE">&nbsp;</td>
					<td bgcolor="#EEEEEE" align="center"><input name="answer_'.$question['id'].'[]" type="checkbox" id="answer_'.$question['id'].'[]" value="'.$question['falseb'].'" />
					  <label for="answer_5"></label></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['falseb'].'</td>
				  </tr>';
				  }
				  if($question['falsec'] != ""){
					$display_options_html[] = '<tr>
					<td bgcolor="#EEEEEE">&nbsp;</td>
					<td bgcolor="#EEEEEE" align="center"><input name="answer_'.$question['id'].'[]" type="checkbox" id="answer_'.$question['id'].'[]" value="'.$question['falsec'].'" />
					  <label for="answer_5"></label></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['falsec'].'</td>
				  </tr>';
				  }
				  if($question['falsed'] != ""){
					$display_options_html[] = '<tr>
					<td bgcolor="#EEEEEE">&nbsp;</td>
					<td bgcolor="#EEEEEE" align="center"><input name="answer_'.$question['id'].'[]" type="checkbox" id="answer_'.$question['id'].'[]" value="'.$question['falsed'].'" />
					  <label for="answer_5"></label></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px">'.$question['falsed'].'</td>
				  </tr>';
				  }
				  
				  echo '<tr>
					<td width="150" align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#999"><strong>Question</strong></td>
					<td width="40" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#00F">&nbsp;</td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px;color:#00F">'.$question['question'].'</td>
				  </tr>';
				  
				  $counter = count($display_options_html);
				  for($i = 0; $i < $counter; $i++){
					$random_index = rand(0, count($display_options_html)-1);
					echo $display_options_html[$random_index];
					unset($display_options_html[$random_index]);
					$display_options_html = array_values($display_options_html);
				  }
				}
              ?>
              <tr>
                <td colspan="3" bgcolor="#EEEEEE" align="center"><span style="font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#F00">Do not press the button below until  you finish to answer all the questions.</span></td>
                </tr>
              <tr>
                <td colspan="3" bgcolor="#EEEEEE" align="center"><input name="button" type="submit" class="submit-center" id="button" value="Save Answers" onClick="return confirm('Are you sure you want to proceed. Click cancel to review your answers');" /><input name="current_exam_id" type="hidden" id="current_exam_id" value="<?php echo $_SESSION['exam']['uid']; ?>" /><input name="form_question_type" type="hidden" id="form_question_type" value="multiple" /></td>
                </tr>
            </table>
          </form>
		<?php 
		}
		if($_SESSION['exam']['questions_type'][$_SESSION['exam']['loop']] == "essays"){ 
		?>
          <form id="essays_questions" name="essays_questions" method="post" action="take_test.php">
            <table width="100%" border="0" cellpadding="8" cellspacing="2" id="essay_questions">
              <tr>
                <td colspan="2" align="center" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:18px; color:#999"><strong>Essays Questions</strong></td>
                </tr>
               <?php
				$questions = array_splice($_SESSION['exam']['questions'][$_SESSION['exam']['questions_type'][$_SESSION['exam']['loop']]], 0, $current_questions_to_display);
				$_SESSION['exam']['answered_questions'] = $questions;
				foreach($questions as $question){
				  echo '<tr>
					<td width="150" align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#999"><strong>Essay Instructions</strong></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#00F">'.$question['question'].'</td>
				  </tr>
				  <tr>
					<td align="right" valign="top" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#999"><strong>Essay</strong></td>
					<td bgcolor="#EEEEEE"><textarea name="answer_'.$question['id'].'" cols="45" rows="10" class="form-exam" id="answer_'.$question['id'].'"></textarea></td>
				  </tr>';
				}
              ?>
              <tr>
                <td colspan="2" align="center" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#F00">Do not press the button below until  you finish to answer all the questions.</td>
                </tr>
              <tr>
                <td colspan="2" bgcolor="#EEEEEE" align="center"><input name="button" type="submit" class="submit-center" id="button" value="Save Answers" onClick="return confirm('Are you sure you want to proceed. Click cancel to review your answers');" /><input name="current_exam_id" type="hidden" id="current_exam_id" value="<?php echo $_SESSION['exam']['uid']; ?>" /><input name="form_question_type" type="hidden" id="form_question_type" value="essays" /></td>
                </tr>
              </table>
          </form>
		<?php 
		}
		if($_SESSION['exam']['questions_type'][$_SESSION['exam']['loop']] == "problems"){ 
		?>
          <form id="problems_questions" name="problems_questions" method="post" action="take_test.php">
            <table width="100%" border="0" cellpadding="8" cellspacing="2" id="problem_questions">
              <tr>
                <td colspan="2" align="center" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:18px; color:#999"><strong>Solve Problem Questions</strong></td>
                </tr>
              <?php
				$questions = array_splice($_SESSION['exam']['questions'][$_SESSION['exam']['questions_type'][$_SESSION['exam']['loop']]], 0, $current_questions_to_display);
				$_SESSION['exam']['answered_questions'] = $questions;
				foreach($questions as $question){
				  echo '<tr>
					<td width="150" align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#999"><strong>Problem</strong></td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#00F">'.$question['question'].'</td>
				  </tr>
				  <tr>
					<td align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#999"><strong>Answer</strong></td>
					<td bgcolor="#EEEEEE"><input name="answer_'.$question['id'].'" type="text" class="form-exam" id="answer_'.$question['id'].'" /></td>
				  </tr>';
				}
              ?>
              <tr>
                <td colspan="2" align="center" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#F00">Do not press the button below until  you finish to answer all the questions.</td>
                </tr>
              <tr>
                <td colspan="2" bgcolor="#EEEEEE" align="center"><input name="button" type="submit" class="submit-center" id="button" value="Save Answers" onClick="return confirm('Are you sure you want to proceed. Click cancel to review your answers');" /><input name="current_exam_id" type="hidden" id="current_exam_id" value="<?php echo $_SESSION['exam']['uid']; ?>" /><input name="form_question_type" type="hidden" id="form_question_type" value="problems" /></td>
                </tr>
              </table>
          </form>
		<?php 
		}
		if($_SESSION['exam']['questions_type'][$_SESSION['exam']['loop']] == "truefalse"){ 
		?>
          <form id="truefalse_questions" name="truefalse_questions" method="post" action="take_test.php">
            <table width="100%" border="0" cellspacing="2" cellpadding="8">
              <tr>
                <td colspan="3" align="center" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:18px; color:#999"><strong>True or False Questions</strong></td>
                </tr>
              <?php
				$questions = array_splice($_SESSION['exam']['questions'][$_SESSION['exam']['questions_type'][$_SESSION['exam']['loop']]], 0, $current_questions_to_display);
				$_SESSION['exam']['answered_questions'] = $questions;
				foreach($questions as $question){
				  echo '<tr>
					<td width="150" align="right" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#999"><strong>Question</strong></td>
					<td width="500" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#00F">'.$question['question'].'</td>
					<td bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#00F"><select name="answer_'.$question['id'].'" class="form-course-small" id="answer_'.$question['id'].'">
					  <option value="0" selected="selected">Select Your Answer</option>
					  <option value="true">True</option>
					  <option value="false">False</option>
					  </select></td>
				  </tr>';
				}
			  ?>
              <tr>
                <td colspan="3" bgcolor="#EEEEEE" align="center"><span style="font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#F00">Do not press the button below until  you finish to answer all the questions.</span></td>
                </tr>
              <tr>
                <td colspan="3" bgcolor="#EEEEEE" align="center"><input name="button" type="submit" class="submit-center" id="button" value="Save Answers" onClick="return confirm('Are you sure you want to proceed. Click cancel to review your answers');" /><input name="current_exam_id" type="hidden" id="current_exam_id" value="<?php echo $_SESSION['exam']['uid']; ?>" /><input name="form_question_type" type="hidden" id="form_question_type" value="truefalse" /></td>
                </tr>
              </table>
          </form>
		<?php 
		}
		if($_SESSION['exam']['questions_type'][$_SESSION['exam']['loop']] == "match"){ 
		?>
          <form id="match_questions" name="match_questions" method="post" action="take_test.php">
            <table width="100%" border="0" cellspacing="2" cellpadding="8">
              <tr>
                <td colspan="2" align="center" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:18px; color:#999"><strong>Match Questions</strong></td>
                </tr>
              <tr>
                <td colspan="2" align="center" bgcolor="#EEEEEE" style="font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#00F">Drag and drop the answer on the left to the question on the right, when the question turn red, mean you drag it correctly. The answer and question should match</td>
                </tr>
              <tr>
                <td bgcolor="#EEEEEE" align="center" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#999"><strong>Answer</strong></td>
                <td bgcolor="#EEEEEE" align="center" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#999"><strong>Question</strong></td>
              </tr>
              <?php
				$shuffle_questions = $questions;
				shuffle($shuffle_questions);
				$counter = 0;
				foreach($questions as $question){
					echo '<tr>
					<td width="50%" bgcolor="#EEEEEE" align="left"><div id="draggable_'.$counter.'" class="draggable">'.$question['answer'].'</div></td>';
					
					echo '<td width="50%" bgcolor="#EEEEEE" align="right"><div id="droppable_'.$shuffle_questions[$counter]['id'].'" class="droppable">'.$shuffle_questions[$counter]['question'].'</div>
	  <input name="answer_'.$shuffle_questions[$counter]['id'].'" type="hidden" id="answer_'.$shuffle_questions[$counter]['id'].'" value="'.$shuffle_questions[$counter]['id'].'" /></td>
				  </tr>';
				  $counter++;
				}
              ?>
              <tr>
                <td colspan="2" align="center" bgcolor="#EEEEEE"><span style="font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#F00">Do not press the button below until  you finish to answer all the questions.</span></td>
              </tr>
              <tr>
                <td colspan="2" align="center" bgcolor="#EEEEEE"><input name="button" type="submit" class="submit-center" id="button" value="Save Answers" onClick="return confirm('Are you sure you want to proceed. Click cancel to review your answers');" /><input name="current_exam_id" type="hidden" id="current_exam_id" value="<?php echo $_SESSION['exam']['uid']; ?>" /><input name="form_question_type" type="hidden" id="form_question_type" value="match" /></td>
                </tr>
            </table>
          </form>
		<?php 
		}
		?>
         </td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="30" style=" background-image:url(../images/bottom-bg-student.png); background-repeat:repeat-x;">&nbsp;</td>
  </tr>
  <tr>
    <td height="50" align="center" bgcolor="#000000" style="color:#fff; font-family:Arial, Helvetica, sans-serif">PIE.EDU All Rights Reserved 2012<br />
      <a href="/common/privacy.php" class="bottom-menu" style="color:#999">Privacy Policy</a> | <a href="/common/terms.php" class="bottom-menu"  style="color:#999">Terms of Use</a> | <a href="/help/main.php" class="bottom-menu" style="color:#999">Help Center</a> | <a href="/common/jobs.php" class="bottom-menu" style="color:#999">Jobs</a></td>
  </tr>
</table>
</body>
</html>